package com.one.square.file.server.controller;

import com.one.square.core.domain.Result;
import com.one.square.core.entity.FileInfo;
import com.one.square.file.interfaces.service.service.FileInterFace;
import com.one.square.file.server.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author cwm
 * @Description
 * @date 2021/10/27 下午3:07
 * @Version 1.0
 */
@RestController
@RequestMapping("/file")
@Api(value = "/file/", tags = {"文件系统功能"})
public class FileController implements FileInterFace {
    @Resource
    private IFileService fileService;
    /**
     * 文件上传
     * 根据fileType选择上传方式
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/files-anon")
    @ApiOperation(value = "文件上传")
    public Result<FileInfo> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return Result.succeed(fileService.upload(file));
    }


    /**
     * 文件删除
     *
     * @param id
     */
    @DeleteMapping("/files/{id}")
    @ApiOperation(value = "文件删除")
    public Result delete(@PathVariable String id) {
        try {
            fileService.delete(id);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            return Result.failed("操作失败");
        }
    }

    @PutMapping("/set/is/true")
    @ApiOperation(value = "设置文件为有效资源")
    public Result setFileIsTrue(String id){
        fileService.setFileIsTrue(id);
        return Result.succeed("操作成功");
    }
    /**
     * 文件查询
     *
     * @param params
     * @return
     */
   /* @GetMapping("/files")
    public PageResult<FileInfo> findFiles(@RequestParam Map<String, Object> params) {
        return fileService.findList(params);
    }*/
}
